package com.hospital.ecard.xmlutils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author lvyao
 * @description 远程请求类
 * @date 2020/7/1
 */
public class SoapUtil {
    public static String getWebServiceAndSoap(String sendUrl, StringBuffer sendSoapString) throws Exception {
//        String soap=sendSoapString.toString();
////        if(soap==null){
////            return null;
////        }
////        URL soapUrl=new URL(url);
////        URLConnection conn=soapUrl.openConnection();
////        conn.setUseCaches(false);
////        conn.setDoInput(true);
////        conn.setDoOutput(true);
////        conn.setRequestProperty("Content-Length",Integer.toString(soap.length()));
////        conn.setRequestProperty("Content-Type","text/xml;charset=utf-8");
////        conn.setRequestProperty(isClass,isMethod);
////        OutputStream os=conn.getOutputStream();
////        OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
////        osw.write(soap);
////        osw.flush();
////        osw.close();
////        InputStream is=conn.getInputStream();
////        if(is!=null){
////            byte[] bytes=new byte[is.available()];
////            is.read(bytes);
////            String str=new String(bytes,0,bytes.length,"utf-8");
////            return str;
////        }else {
////            return null;
////        }
        InputStreamReader isr = null;
        BufferedReader inReader = null;
        StringBuffer result = null;
        OutputStream outObject = null;
        try {
            URL url = new URL(sendUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

            // 设置HTTP请求相关信息
            httpConn.setRequestProperty("Content-Length",
                    String.valueOf(sendSoapString.toString().getBytes().length));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setConnectTimeout(10000);
            httpConn.setReadTimeout(10000);

            // 进行HTTP请求
            outObject = httpConn.getOutputStream();
            outObject.write(sendSoapString.toString().getBytes("UTF-8"));

            if (200 != (httpConn.getResponseCode())) {
                throw new Exception("HTTP Request is not success, Response code is " + httpConn.getResponseCode());
            }
            // 获取HTTP响应数据
            isr = new InputStreamReader(
                    httpConn.getInputStream(), "utf-8");
            inReader = new BufferedReader(isr);
            result = new StringBuffer(1024);
            String inputLine;
            while ((inputLine = inReader.readLine()) != null) {
                result.append(inputLine);
            }
            return result.toString();
        } catch (IOException e) {
            throw e;
        } finally {
            if (inReader != null) {
                inReader.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (outObject != null) {
                outObject.close();
            }
        }
    }

    public static <T> T getWebReturnResult(String sendUrl, StringBuffer sendSoapString, String elementName, Class<T> cls) throws Exception {
        String resultStr = getWebServiceAndSoap(sendUrl, sendSoapString);
        return XmlUtils.xmlToBean(resultStr, elementName, cls);
    }
}
